PROJECT_NAME := fr8000_project

CC := arm-none-eabi

TOP_DIR := ../../../../..
PROJECT_DIR := ../code

LD         := $(TOP_DIR)/components/toolchain/gcc/ldscript.ld
LD_C       := $(TOP_DIR)/components/ble/library/syscall_gcc.txt
LIB        := $(TOP_DIR)/components/ble/library/libfr800x_stack.a
OBJECT_DIR := Objects

# ---------------------------------- #
# Source files common to all targets #
# ---------------------------------- #
SRC_FILES += $(OBJECT_DIR)/boot_vectors.o
SRC_FILES += $(OBJECT_DIR)/driver_pmu.o
SRC_FILES += $(OBJECT_DIR)/driver_gpio.o


SRC_FILES += $(OBJECT_DIR)/proj_main.o
SRC_FILES += $(OBJECT_DIR)/co_log.o
SRC_FILES += $(OBJECT_DIR)/prf_client.o
SRC_FILES += $(OBJECT_DIR)/prf_server.o
SRC_FILES += $(OBJECT_DIR)/user_task.o
SRC_FILES += $(OBJECT_DIR)/gpio_demo.o

# ---------------------------------- #
# Source files path                  #
# ---------------------------------- #
VPATH += $(TOP_DIR)/components/toolchain/gcc
VPATH += $(TOP_DIR)/components/driver
VPATH += $(TOP_DIR)/components/driver/components/display
VPATH += $(TOP_DIR)/components/driver/components/spi_flash
VPATH += $(TOP_DIR)/components/driver/components/touchpad
VPATH += $(TOP_DIR)/components/modules/common/src

VPATH += $(PROJECT_DIR)

# ---------------------------------- #
# Include header files               #
# ---------------------------------- #
INC_PATH += -I"$(TOP_DIR)/components/driver/include"
INC_PATH += -I"$(TOP_DIR)/components/driver/drv_common"
INC_PATH += -I"$(TOP_DIR)/components/driver/components/display"
INC_PATH += -I"$(TOP_DIR)/components/driver/components/spi_flash"
INC_PATH += -I"$(TOP_DIR)/components/driver/components/touchpad"
INC_PATH += -I"$(TOP_DIR)/components/modules/platform/include"
INC_PATH += -I"$(TOP_DIR)/components/modules/platform/include/cmsis"
INC_PATH += -I"$(TOP_DIR)/components/modules/common/include"
INC_PATH += -I"$(TOP_DIR)/components/modules/sys/include"
INC_PATH += -I"$(TOP_DIR)/components/modules/os/include"
INC_PATH += -I"$(TOP_DIR)/components/ble/include"
INC_PATH += -I"$(TOP_DIR)/components/ble/include/gap"
INC_PATH += -I"$(TOP_DIR)/components/ble/include/gatt"
INC_PATH += -I"$(TOP_DIR)/components/ble/include/mesh"

INC_PATH += -I"$(PROJECT_DIR)"

# ---------------------------------- #
# C flags common to all targets      #
# ---------------------------------- #
CFLAGS += -mcpu=cortex-m3
CFLAGS += -mthumb
CFLAGS += -O0
CFLAGS += -fmessage-length=0 -fsigned-char
CFLAGS += -ffunction-sections -fdata-sections
CFLAGS += -g3
CFLAGS += -std=gnu11

# ---------------------------------- #
# LD flags common to all targets     #
# ---------------------------------- #
LDFLAGS += -mcpu=cortex-m3
LDFLAGS += -mthumb
LDFLAGS += -O0
LDFLAGS += -fmessage-length=0 -fsigned-char
LDFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -g3
LDFLAGS += -Xlinker --gc-sections
LDFLAGS += --specs=nosys.specs

# ---------------------------------- #
# build target                       #
# ---------------------------------- #
all: path target

path :
	if not exist $(OBJECT_DIR) md $(OBJECT_DIR)

target : $(SRC_FILES)
	$(CC)-gcc $(LDFLAGS) -T $(LD) -T $(LD_C) -Wl,-Map,"$(PROJECT_NAME).map" -o $(OBJECT_DIR)/$(PROJECT_NAME).elf $^ $(LIB)
	$(CC)-objcopy -O binary -S $(OBJECT_DIR)/$(PROJECT_NAME).elf $(PROJECT_NAME).bin
	$(CC)-objcopy -O ihex   -S $(OBJECT_DIR)/$(PROJECT_NAME).elf $(PROJECT_NAME).hex
	$(CC)-objdump -D $(OBJECT_DIR)/$(PROJECT_NAME).elf > $(PROJECT_NAME).dis
	
$(OBJECT_DIR)/%.o : %.c
	$(CC)-gcc $(CFLAGS) $(INC_PATH) -c -o $@ $<
$(OBJECT_DIR)/%.o : %.s
	$(CC)-gcc $(CFLAGS) $(INC_PATH) -c -o $@ $<

# ---------------------------------------- #
# delete file(.o .elf .bin .dis .hex .map) #
# ---------------------------------------- #
clean :
	del $(OBJECT_DIR)\*.o $(OBJECT_DIR)\*.elf *.bin *.dis *.hex *.map

